function SpDataView(clientName, id, currentPageIndexId, currentPageSizeId, enable) {
  var _dataCallbackFunction = "3";
  var _customCallbackFunction = "2";
  var _pageIndexChangeFunction = "1";
  var _hashParameter = Library.WebControl.Hashtable.CreateHashtable();
  var _util = Library.WebControl.Util;
  var _instance = this;
  var _id = id;
  var _currentPageIndexId = currentPageIndexId;
  var _currentPageSizeId = currentPageSizeId;
  var _enable = enable;
  var _beforeCallback = null;
  _globalHashObject.Add(clientName, _instance);
  window[clientName] = _instance;

  this.Visible = function() {
    var d = _util.GetObjectById(_id);
    if (arguments.length > 0) {
      var display = arguments[0] ? "" : "none";
      d.style.display = display;
    }
    return d.style.display != "none";
  };

  this.Enable = function() {
    if (arguments.length > 0) {
      var d = _util.GetObjectById(_id);
      _enable = arguments[0];
      if (_enable) {
        d.removeAttribute("disabled");
      }
      else {
        d.setAttribute("disabled", "disabled");
      }
    }
    return _enable;
  };


  function commonCallback(callbackFunction, callbackParameter, currentPageIndex, currentPageSize, callbackFunctionResult) {
    if (!_enable)
      return;
    if (_beforeCallback != null)
      _beforeCallback(_instance, {
        CallbackType: callbackFunction,
        Parameter: callbackParameter
      });
    if (_dataCallbackFunction != callbackFunction) {
      var parentData = _util.GetObjectById(_id);
      parentData = parentData.getElementsByTagName("div")[0];
      _util.ShowLoadingPanel({
        Parrent: parentData,
        LoadingId: _id + '_LDP'
      });
    }
    var ajaxRequest = Library.WebControl.Ajax.CreateRequest();
    ajaxRequest.SetData('__SPDATAVIEWRQP_=T&__SPDATAVIEWRQF_=' + callbackFunction + '&__SPDATAVIEWRQD_=' + encodeURIComponent(callbackParameter) + '&__SPDATAVIEWPID_=' + currentPageIndex + '&__SPDATAVIEWPS_=' + currentPageSize + '&__SPDATAVIEWRQSID_=' + _id + '&' + _hashParameter.ToQuery());
    _hashParameter.Clear();
    ajaxRequest.SendCallback({
      P: callbackParameter,
      T: callbackFunction,
      F: callbackFunctionResult
    }, callbackComplete);
  };

  function pagingTextBox() {
    var grid = _util.GetObjectById(_id);
    var spgvDivPaging = _util.FindFirstChild(grid, "div", "spgvDivPaging");
    if (spgvDivPaging != null) {
      return spgvDivPaging.getElementsByTagName("input");
    }
    return null;
  };
  function curPageIndex() {
    return findPagingTextBox("current-page-index");
  };
  function curPageSize() {
    return findPagingTextBox("current-page-size");
  };
  function findPagingTextBox(clzz) {
    var ips = pagingTextBox();
    if (ips != null) {
      for (var i = 0; i < ips.length; i++) {
        var ip = ips[i];
        var type = ip.type;
        if (!type) type = ip.getAttribute("type");
        if (type) type = type.toLowerCase();
        if (type == "text") {
          var czz = _util.GetClass(ip);  //type.className;
          //if (!czz) czz = ip.getAttribute("class");
          if (czz == clzz) return ip;
        }
      }
    }
    return null;
  };

  this.PageIndex = function() {
    var p = curPageIndex(); //_util.GetObjectById(_currentPageIndexId);
    if (p == null) return 0;
    if (arguments.length > 0) {
      p.value = arguments[0];
    }
    return p.value;
  };

  this.PageSize = function() {
    var p = curPageSize(); //_util.GetObjectById(_currentPageSizeId);
    if (p == null) return 0;
    if (arguments.length > 0) {
      p.value = arguments[0];
    }
    return p.value;
  };

  this.SendDataCallback = function(parameter, callbackFunction) {
    var currentPageIndex = _instance.PageIndex();
    var currentPageSize = _instance.PageSize();
    commonCallback(_dataCallbackFunction, parameter, currentPageIndex, currentPageSize, callbackFunction);
  };
  this.SendCallback = function(parameter, callbackFunction) {
    var currentPageIndex = _instance.PageIndex();
    var currentPageSize = _instance.PageSize();
    commonCallback(_customCallbackFunction, parameter, currentPageIndex, currentPageSize, callbackFunction);
  };
  function callbackComplete(sender, e) {
    _util.HideLoadingPanel(_id + '_LDP');
    var argsComplete = _util.ParseAjaxResponse(e.ResponseText);
    argsComplete.Parameter = sender.P;
    if (!argsComplete.IsValid) {
      alert(argsComplete.Result);
    }
    else {
      switch (sender.T) {
        case _pageIndexChangeFunction:
        case _customCallbackFunction:
          _util.EvalScriptAndHtml(argsComplete.Data, _id);
          break;

      }
    }
    if (sender.F && typeof (sender.F) == "function") {
      sender.F(_instance, argsComplete);
    }
  };


  this.GetPage = function(pageIndex) {
    var currentPageSize = _instance.PageSize();
    commonCallback(_pageIndexChangeFunction, "", pageIndex, currentPageSize, function(s, e) {
    });
  };
  this.Refresh = function() {
    var currentPageIndex = _instance.PageIndex();
    _instance.GetPage(currentPageIndex);
  };

  function keyPress(e) {
    if (!_enable) return false;
    return _util.HandleEnterPress(e, function() { _instance.Refresh(); });
  };
  this.Call = function(t, p) {
    switch (t) {
      case 1: //key press
        return keyPress(p);
    }
  };
  this.AddHandler = function(t, f) {
    switch (t) {
      case 1:
        _beforeCallback = f;
        break;
    }

  };

  this.AddParameter = function(k, v) {
    _hashParameter.Add(k, v);
  };
};
